package org.mountain.music.util;

import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.util.Log;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2018\9\12 0012.
 */

public class MusicUtils {

    private static List<Music> musiclist;
    private static Music music;


    public static List<Music> getMusiclist(Context context) {
        musiclist = new ArrayList<>();

        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null, MediaStore.Audio.AudioColumns.IS_MUSIC);

        if (cursor != null) {

            while (cursor.moveToNext()) {
                music = new Music();
                music.song = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DISPLAY_NAME));
                music.singer = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
                music.path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
                music.duration = cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION));
                music.size = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE));
                Log.e("miusic list",music.path);
                musiclist.add(music);
           /*     if (music.size > 1000 * 800) {

                    if (music.song.contains("-")) {

                        String[] str = music.song.split("-");
                        music.singer = str[0];
                        music.song = str[1];

                        musiclist.add(music);

                    }
                }*/


            }
        }


        cursor.close();
        return musiclist;
    }

    public static String formatTime(int time) {
        if (time / 1000 % 60 < 10) {
            String tt = time / 1000 / 60 + ":0" + time / 1000 % 60;
            return tt;
        } else {
            String tt = time / 1000 / 60 + ":" + time / 1000 % 60;
            return tt;
        }
    }

}
